'use strict';

const Service = require('egg').Service;
const sd = require('silly-datetime');
// 使用
// const moment = require('moment');
const uuid = require('uuid');
class ReservationService extends Service {
  async add (reservation, transaction) {
    console.log(reservation);
    reservation.id = uuid.v1().replace(new RegExp('-', 'gm'), '');
    reservation.myOrder = sd.format(new Date(), 'YYYYMMDDHHmmss');
    reservation.status = '1';
    const reser = await this.app.model.Reservation.create(reservation, {
      transaction,
    });
    return reser;
  }

  async getMessages (data, transaction) {
    console.log(data);
    console.log('----------getMessages------------');


    // 获取通告数据
    const { QueryTypes } = require('sequelize');
    const list = await this.app.model.query(
      //  'SELECT * from reservation WHERE hugong_id=:id AND `status`=:status',
      ' SELECT r.*,u.img from reservation r,user u WHERE r.hugong_id=:id AND r.`status`=:status AND r.user_id = u.id',
      {
        replacements: { id: data.id, status: data.status },
        type: QueryTypes.SELECT,
      }
    );
    return list;
  }
  async getMessagesByShouhuo (data, transaction) {
    console.log(data);
    console.log('----------getMessages------------');
    // 获取通告数据
    const { QueryTypes } = require('sequelize');
    const list = await this.app.model.query(
      'SELECT * from reservation WHERE user_id=:id AND `status`=:status',
      {
        replacements: { id: data.id, status: data.status },
        type: QueryTypes.SELECT,
      }
    );
    return list;
  }
  async getOrderDetail (my_order, transaction) {
    console.log(my_order);
    console.log('----------getOrderDetail------------');
    // 获取通告数据
    const { QueryTypes } = require('sequelize');
    const list = await this.app.model.query(
      'SELECT r.*,u.img from reservation r, user u WHERE my_order=:my_order AND u.id=r.user_id',
      {
        replacements: { my_order },
        type: QueryTypes.SELECT,
      }
    );
    return list;
  }

  async saveResult (data, transaction) {
    console.log('*********************************saveResult*******************************');
    const result = await this.app.model.Reservation.update(data, {
      where: {
        id: data.id,
      }, transaction,
    });
    return result;
  }
}
module.exports = ReservationService;
